#!/bin/bash

# 远程服务器启动和验证脚本
# 用法: ssh asr@192.168.10.11 'bash -s' < remote_startup_and_verify.sh

set -e

# 配置
REMOTE_PATH="/home/asr/git/paddle-ocr-webcam"
CONDA_ENV="ov"
SERVICE_PORT="8000"
SERVICE_URL="http://localhost:${SERVICE_PORT}"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# 打印函数
print_header() {
    echo -e "${BLUE}╔════════════════════════════════════════════════════════════╗${NC}"
    echo -e "${BLUE}║${NC} $1"
    echo -e "${BLUE}╚════════════════════════════════════════════════════════════╝${NC}"
}

print_success() {
    echo -e "${GREEN}✅ $1${NC}"
}

print_error() {
    echo -e "${RED}❌ $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}⚠️  $1${NC}"
}

print_info() {
    echo -e "${BLUE}ℹ️  $1${NC}"
}

# 进入项目目录
cd "$REMOTE_PATH"

print_header "远程服务器启动和验证"

# 激活 conda 环境
print_info "激活 conda 环境: $CONDA_ENV"
source /home/asr/anaconda3/etc/profile.d/conda.sh
conda activate $CONDA_ENV

# 验证代码
print_header "1️⃣  代码验证"

if grep -q "process-video-text-only" main.py; then
    print_success "/process-video-text-only 接口已添加"
else
    print_error "/process-video-text-only 接口未找到"
    exit 1
fi

if grep -q "process_video_text_only" video_processor.py; then
    print_success "process_video_text_only() 方法已添加"
else
    print_error "process_video_text_only() 方法未找到"
    exit 1
fi

# 检查 Python 语法
print_header "2️⃣  Python 语法验证"

if python -m py_compile main.py 2>/dev/null; then
    print_success "main.py 语法正确"
else
    print_error "main.py 语法错误"
    exit 1
fi

if python -m py_compile video_processor.py 2>/dev/null; then
    print_success "video_processor.py 语法正确"
else
    print_error "video_processor.py 语法错误"
    exit 1
fi

# 检查依赖
print_header "3️⃣  依赖验证"

packages=("fastapi" "starlette" "cv2" "numpy" "paddle")
for package in "${packages[@]}"; do
    if python -c "import $package" 2>/dev/null; then
        print_success "$package 已安装"
    else
        print_warning "$package 未安装"
    fi
done

# 启动服务
print_header "4️⃣  启动 FastAPI 服务"

print_info "启动服务在 $SERVICE_URL"
print_info "按 Ctrl+C 停止服务"
print_info ""

# 启动服务（后台运行）
python main.py > /tmp/fastapi_service.log 2>&1 &
SERVICE_PID=$!

print_success "服务已启动 (PID: $SERVICE_PID)"

# 等待服务启动
print_info "等待服务启动..."
sleep 3

# 检查服务是否运行
if ps -p $SERVICE_PID > /dev/null; then
    print_success "服务进程正在运行"
else
    print_error "服务启动失败"
    cat /tmp/fastapi_service.log
    exit 1
fi

# 测试健康检查
print_header "5️⃣  API 测试"

print_info "测试健康检查..."
if curl -s -X GET "$SERVICE_URL/health" > /dev/null 2>&1; then
    print_success "健康检查通过"
    health_response=$(curl -s -X GET "$SERVICE_URL/health")
    echo "  响应: $health_response"
else
    print_warning "健康检查失败，服务可能还在启动中"
fi

# 显示服务信息
print_header "6️⃣  服务信息"

print_info "服务 URL: $SERVICE_URL"
print_info "服务 PID: $SERVICE_PID"
print_info "API 文档: $SERVICE_URL/docs"
print_info "ReDoc: $SERVICE_URL/redoc"

# 显示可用的 API 端点
print_header "7️⃣  可用的 API 端点"

echo "  GET  /health"
echo "  POST /process-video"
echo "  POST /process-video-with-output"
echo "  POST /process-video-text-only"

# 显示测试命令
print_header "8️⃣  测试命令"

echo ""
echo "基础测试:"
echo "  curl -X GET '$SERVICE_URL/health'"
echo ""
echo "文本识别接口测试:"
echo "  curl -X POST '$SERVICE_URL/process-video-text-only' \\"
echo "    -F 'file=@test.mp4'"
echo ""
echo "带参数的测试:"
echo "  curl -X POST '$SERVICE_URL/process-video-text-only' \\"
echo "    -F 'file=@test.mp4' \\"
echo "    -F 'sample_rate=5' \\"
echo "    -F 'min_score=0.8'"
echo ""

# 显示日志
print_header "9️⃣  服务日志"

echo "最近的日志输出:"
tail -20 /tmp/fastapi_service.log

# 显示下一步
print_header "📞 下一步"

echo ""
echo "1. 在另一个终端中测试接口:"
echo "   ssh asr@192.168.10.11"
echo "   curl -X GET 'http://localhost:8000/health'"
echo ""
echo "2. 查看 API 文档:"
echo "   访问 http://192.168.10.11:8000/docs"
echo ""
echo "3. 停止服务:"
echo "   kill $SERVICE_PID"
echo ""
echo "4. 查看详细文档:"
echo "   cat REMOTE_TEST_GUIDE.md"
echo ""

# 保持服务运行
print_header "✅ 验证完成"

echo ""
echo "服务已启动并通过验证！"
echo "服务将继续运行，按 Ctrl+C 停止"
echo ""

# 等待用户中断
wait $SERVICE_PID

